<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<h1>原生ajax</h1>
<input type="text" id="userName" value="hehe">
<button id="getbtn">ajax--get</button>
<input type="text" id="pswd">
<button id="postbtn"></button>
<script>
    let getBtn = document.querySelector('#getbtn');
    let postBtn = document.querySelector('#postbtn');
    let userName = document.querySelector('#userName');
    let pswd = document.querySelector('#pswd');

    // 原生ajax-get
    getBtn.onclick = function () {
        // 1.创建请求对象
        let xhr = new XMLHttpRequest();
        // 2.创立链接
        xhr.open("get", `/login?name=${userName.value}&pswd=${pswd.value}`, true)
        // 3.发送请求
        xhr.send();
        // 4.监听请求状态
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                console.log("ajax-get", xhr.responseText);
            }
        }
    }
    // 原生ajax-post
    postBtn.onclick = function () {
        // 1.创建请求对象
        let xhr = new XMLHttpRequest();
        // 2.创立链接
        xhr.open("post", "/register", true)
        // 3.发送请求
        xhr.send(`name=${userName.value}&pswd=${pswd.value}`);
        // 4.监听请求状态
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                console.log("ajax-post", xhr.responseText);
            }
        }
    }
</script>
</body>
</html>